package com.liesheng.haylou.ui.device;

import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

import androidx.databinding.DataBindingUtil;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.bluetooth.BleManager;
import com.liesheng.haylou.databinding.ActivitySelectDeviceBinding;
import com.liesheng.haylou.event.BleDeviceEvent;
import com.liesheng.haylou.ui.device.vm.SelectDeviceVm;
import com.liesheng.haylou.ui.main.MainActivity;
import com.liesheng.haylou.utils.LogUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

/**
 * Date:${DATA}
 * Author ${ZhangZhiQiang}
 * Description
 * 选择设备
 */
public class SelectDeviceActivity extends BaseActivity<ActivitySelectDeviceBinding, SelectDeviceVm> {
    @Override
    protected ActivitySelectDeviceBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_select_device, null, false);
        return mBinding;
    }

    @Override
    protected SelectDeviceVm getViewModel() {
        return new SelectDeviceVm(this);
    }

    @Override
    protected void initData() {
        setTitle(R.string.choose_device);
        setStatusBarColor(R.color.main_bg, false);
        startProgressAnimation();
        mVm.init();
    }

    //扫描进度
    private void startProgressAnimation() {

        long curTime = System.currentTimeMillis();
        long startTime = getIntent().getLongExtra("startScanTime", curTime);
        long lastTime = curTime - startTime;
        float start = new BigDecimal((float) lastTime / BleManager.SCAN_DURING).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        long dur = BleManager.SCAN_DURING - lastTime;
        ScaleAnimation scaleAnimation = new ScaleAnimation(start, 1, 1, 1);
        scaleAnimation.setDuration(dur);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mBinding.viewAnimation.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mBinding.viewAnimation.setAnimation(scaleAnimation);
        scaleAnimation.start();
    }

    public static void startBy(BaseActivity activity, long startScanTime) {
        Intent intent = new Intent(activity, SelectDeviceActivity.class);
        intent.putExtra("startScanTime", startScanTime);
        activity.startActivity(intent);
    }

    @Override
    protected boolean isRegistEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventPost(BleDeviceEvent event) {
        mVm.addData(event.bleDataInfo);
    }

    @Override
    public void onClickView(View view) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().stopScan();
    }

    @Override
    protected void onClickLeft() {
        MainActivity.startBy(this);
        finish();
    }

    @Override
    public void onBackPressed() {
        onClickLeft();
    }
}
